package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.entity.PerformanceAnalysis;
import pers.cj.pdos.entity.ProfitsReport;
import pers.cj.pdos.entity.SaleAnalysis;
import pers.cj.pdos.mapper.PerformanceAnalysisMapper;
import pers.cj.pdos.mapper.ProfitsReportMapper;
import pers.cj.pdos.service.IPerformanceAnalysisService;
import pers.cj.pdos.service.IProfitsReportService;
import pers.cj.pdos.utils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class PerformanceAnalysisServiceImpl extends ServiceImpl<PerformanceAnalysisMapper, PerformanceAnalysis> implements IPerformanceAnalysisService {
    @Autowired
    PerformanceAnalysisMapper performanceAnalysisMapper;

    @Override
    public Map getTotalData(String companyId, String storeId, Date date1, Date date2) {
        return performanceAnalysisMapper.getTotalData(companyId, storeId, date1, date2);
    }

    @Override
    public Map getHighSaleUser(String companyId, String storeId, Date date1, Date date2) {
        return performanceAnalysisMapper.getHighSaleUser(companyId, storeId, date1, date2);
    }

    @Override
    public List<PerformanceAnalysis> listUserData(String companyId,
                                                  Integer page,
                                                  Integer limit,
                                                  String storeId,
                                                  Date date1,
                                                  Date date2,
                                                  Boolean filterZero,
                                                  String search,
                                                  String userStoreId) {
        List<PerformanceAnalysis> list = performanceAnalysisMapper.listUserData(companyId, Utils.genPage(page,limit), limit, storeId, date1, date2, filterZero, search,userStoreId);
        BigDecimal total = performanceAnalysisMapper.getUserTotal(companyId, storeId, date1, date2, filterZero, search);
        if (!Utils.isEmpty(list) && total.compareTo(new BigDecimal(0))!=0) {
            List<PerformanceAnalysis> newList = new ArrayList<>();
            for (PerformanceAnalysis entity:list) {
                entity.setRatio(entity.getAmount().multiply(new BigDecimal(100)).divide(total,4, RoundingMode.HALF_UP));
                newList.add(entity);
            }
            return newList;
        }
        return list;
    }

    @Override
    public Long countUserData(String companyId,
                              String storeId,
                              Date date1,
                              Date date2,
                              Boolean filterZero,
                              String search,
                              String userStoreId) {
        return performanceAnalysisMapper.countUserData(companyId, storeId, date1, date2, filterZero, search,userStoreId);
    }

    @Override
    public BigDecimal getUserTotal(String companyId, String storeId, Date date1, Date date2, Boolean filterZero, String search) {
        return performanceAnalysisMapper.getUserTotal(companyId, storeId, date1, date2, filterZero, search);
    }

    @Override
    public List<PerformanceAnalysis> listDetailByOrder(String companyId, Integer page, Integer limit, String userId, String storeId, Date date1, Date date2, String search) {
        return performanceAnalysisMapper.listDetailByOrder(companyId, Utils.genPage(page, limit), limit, userId, storeId, date1, date2, search);
    }

    @Override
    public Long countDetailByOrder(String companyId, String userId, String storeId, Date date1, Date date2, String search) {
        return performanceAnalysisMapper.countDetailByOrder(companyId, userId, storeId, date1, date2, search);
    }

    @Override
    public List<PerformanceAnalysis> listDetailByGoods(String companyId, Integer page, Integer limit, String userId, String storeId, Date date1, Date date2, String search) {
        return performanceAnalysisMapper.listDetailByGoods(companyId, Utils.genPage(page, limit), limit, userId, storeId, date1, date2, search);
    }

    @Override
    public Long countDetailByGoods(String companyId, String userId, String storeId, Date date1, Date date2, String search) {
        return performanceAnalysisMapper.countDetailByGoods(companyId, userId, storeId, date1, date2, search);
    }
}
